import apis from '@/apis/modules/base'
import { generatorDynamicRouter } from '@/router/generate-routers'

export default {
  namespaced: true,
  state: {
    asyncRoutes: []

  },
  mutations: {
    SET_AsyncRoute: (state, routes) => {
      state.asyncRoutes = routes
    },

  },
  actions: {
    //调用后台接口得到可访问菜单列表,生成路由
    generateRoutes({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const menures = await apis.getSystemMenu()
          const routers = await generatorDynamicRouter(menures.data)
          commit('SET_AsyncRoute', routers)
          resolve(routers)
        } catch (error) {
          reject(error)
        }
      })
    },

  }
}